home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7972 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: news.umbc.edu!not-for-mail
  2. From: schlein@umbc.edu (Jonas J. Schlein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Type casting
  5. Date: 29 Feb 1996 17:52:06 -0500
  6. Organization: University of Maryland Baltimore County
  7. Message-ID: <4h5amm$dpv@umbc9.umbc.edu>
  8. References: <4gfnmi$gsc@calvin.risq.qc.ca>
  9. NNTP-Posting-Host: umbc9.umbc.edu
  10. NNTP-Posting-User: schlein
  11.  
  12. Pierre Coulombe <pcoulomb@criq.qc.ca> wrote:
  13. |> I have a problem with type casting in Visual C 1.5.
  14. |> I expected the following program to print the value 254 for valI.
  15. |> Instead it gives the output shown below.
  16. |> Can someone tell me where is my mistake ?
  17. |> 
  18. |> 
  19. |> ******** Program *********
  20. |> 
  21. |> #include <stdio.h>
  22. |> #include <string.h>
  23. |> #include <stdlib.h>
  24. |> 
  25. |> typedef unsigned int UINT;
  26. |> 
  27. |> #define MM_TO_UNITS 10.0F
  28. |> 
  29. |> void main(void)
  30.  
  31. That's a no-no on c.l.c...Please read the FAQ and then you will fully
  32. understand why for your program a correct definition is 'int main (void)'.
  33.  
  34. |> {
  35. |>   float valF;
  36. |>   UINT  valI;
  37. |>   char string[80];
  38. |> 
  39. |>   strcpy(string, "25.4");
  40. |> 
  41. |>   valI =  (UINT) ((float) atof(string) * MM_TO_UNITS);
  42.  
  43. Maybe if you used doubles your problem would go away and maybe not.
  44. Try to write 0.4 out in binary and you'll probably have a hard time
  45. or need a lot of bits. So 25.4 may really be 25.3999999... for all
  46. we know. Type casting will round down for positive numbers.
  47.  
  48. |>   printf("valI = %u\n", valI);
  49. |> 
  50. |>   sprintf(string, "%f", (float) atof(string) * MM_TO_UNITS);
  51.  
  52. I guess this will work, but I'd be skeptical since you are reading
  53. from string while also using it for writing. I don't have my references
  54. near by so just take this as something to think about.
  55.  
  56. |>   printf("string = %s\n", string);
  57. |> 
  58. |>   strcpy(string, "25.4");
  59. |>   sprintf(string, "%u", (UINT) ((float) atof(string) * MM_TO_UNITS));
  60.  
  61. See two comments up.
  62.  
  63. |>   printf("string = %s\n", string);
  64. |> }
  65. |> 
  66. |> 
  67. |> ******** Output *********
  68. |> 
  69. |> valI = 253
  70. |> string = 254.000000
  71. |> string = 253
  72.  
  73. So basically it's a matter of computer precision. I ran your program as is
  74. and was unable to reproduce your results on a UNIX platform. However, I
  75. suspect that is due to floats being a bigger bit size. Just a nitpick, but
  76. you never use the variable valF in your code although it is declared.
  77. -- 
  78. "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
  79.  
  80. Jonas J. Schlein  (schlein@gl.umbc.edu)
  81.